/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#ifndef _HDFS_LIBHDFS3_GF256_H_
#define _HDFS_LIBHDFS3_GF256_H_

#include <iostream>
#include <string>
#include <vector>

namespace Hdfs {
namespace Internal {

class GF256 {
    class GfMulTab {
    public:
        GfMulTab() {
            innerGfMulTab = std::vector<std::vector<int8_t>>(256);
            for (int i = 0; i < 256; i++) {
                innerGfMulTab[i].resize(256);
                for (int j = 0; j < 256; j++) {
                    innerGfMulTab[i][j] = GF256::gfMul((int8_t) i, (int8_t) j);
                }
            }
        }
        const std::vector<std::vector<int8_t>> & getInnerGfMulTab() const {
            return innerGfMulTab;
        }
    private:
        std::vector<std::vector<int8_t>> innerGfMulTab;
    };

public:
    GF256(/* args */) = default;
    static int8_t gfInv(int8_t a);
    static void gfInvertMatrix(std::vector<int8_t> & inMatrix, std::vector<int8_t> & outMatrix, int n);
    static int8_t gfMul(int8_t a, int8_t b);
    static void gfVectMulInit(int8_t c, std::vector<int8_t> & tbl, int offset);

    static const std::vector<std::vector<int8_t>> & getInstance();
    static std::vector<int8_t> GF_BASE;
    static std::vector<int8_t> GF_LOG_BASE;
};

}
}

#endif /* _HDFS_LIBHDFS3_GF256_H_ */
